package com.ysd.lis.controller.lab;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileNameUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.lis.annotation.LogDetail;
import com.ysd.lis.annotation.MainInfoLog;
import com.ysd.lis.entity.lab.*;
import com.ysd.lis.mapper.lab.LabInstrSetupMapper;
import com.ysd.lis.request.AddLabMaininfo;
import com.ysd.lis.request.RptresultImageDto;
import com.ysd.lis.request.lab.LabRptImageTempParam;
import com.ysd.lis.service.lab.LabRptImageTempService;
import com.ysd.lis.aspect.OperationType;
import com.ysd.lis.service.lab.LabRptresultService;
import com.ysd.util.PropertySlash;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * 检验结果图片表(LabRptImageTemp)表控制层
 *
 * @author leiming
 * @since 2024-11-15 13:57:36
 */
@RestController
@RequestMapping("/lab/labRptImageTemp")
public class LabRptImageTempController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private LabRptImageTempService labRptImageTempService;

    @Autowired
    private LabInstrSetupMapper labInstrSetupMapper;

    @Autowired
    LabRptresultService labRptresultService;

    @Value("${picturePath}")
    private String picturePath;

    private static final int MAX_POST_SIZE = 20 * 1024 * 1024;

    /**
     * 结果图片
     */
    // public static final String RESULT_IMAGE = "resultImage";
    @ApiOperation("获取检验结果的图片集")
    @GetMapping("/findImagesByMaininfoId")
    public Result findImagesByMaininfoId(RptResultDto rptResultDto) {
        return labRptImageTempService.findImagesByMaininfoId(rptResultDto);
    }

    @ApiOperation("修改或新增检验结果图片集")
    @PostMapping("/saveOrUpdateImages")
    public Result saveOrUpdateImages(@RequestBody LabRptImageTemp labRptImageTemp) {
        return labRptImageTempService.saveOrUpdateImages(labRptImageTemp);
    }

    @ApiOperation("选择检验图片保存到图文")
    @GetMapping("/selectImagesChecked")
    public Result selectImagesChecked(String id) {
        return labRptImageTempService.selectImagesChecked(id);
    }

    @ApiOperation("取消检验图片保存到图文")
    @GetMapping("/unSelectImagesChecked")
    public Result unSelectImagesChecked(String id) {
        return labRptImageTempService.unSelectImagesChecked(id);
    }

    @ApiOperation("批量删除结果图片集")
    @PostMapping("/batchDeleteImages")
    public Result batchDeleteImages(@RequestBody RptresultImageDto rptresultImageDto) {
        return labRptImageTempService.batchDeleteImages(rptresultImageDto.getEptresultImageIds());
    }


    @ApiOperation("上传检验图片")
    @PostMapping("/uploadImage")
    @LogDetail(operationType = OperationType.UPDATE, module = "图文报告上传检验图片")
    public Result uploadSignatureFile(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws IOException {

        String rptGroup = request.getParameter("rptGroup");
        String sampleda = request.getParameter("sampleda");
        String sampleNo = request.getParameter("sampleNo");
        if (StringUtils.isBlank(rptGroup) || StringUtils.isBlank(sampleda) || StringUtils.isBlank(sampleNo)) {
            return Result.fail("参数不能为空");
        }

        String id = request.getParameter("id");
        if (file == null) {
            return Result.fail("文件不能为空");
        }

        MPJLambdaWrapper<LabInstrSetup> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(LabInstrSetup.class)
                .selectAs("ld", LabRptGroup::getRptGroupName, LabInstrSetup::getReportGroupName)
                .leftJoin(LabRptGroup.class, "ld", LabRptGroup::getRptGroup, LabInstrSetup::getRptGroup)
                .eq(LabInstrSetup::getDelFlag, 0)
                .eq(LabRptGroup::getRptGroup, rptGroup);
        List<LabInstrSetup> labInstrSetups = labInstrSetupMapper.selectList(lambdaQueryWrapper);
        String instrCode = "other";
        if (CollectionUtils.isNotEmpty(labInstrSetups)) {
            instrCode = labInstrSetups.get(0).getInstrCode();
        }
        // 文件限制10M
        long size = file.getSize();
        if (size > MAX_POST_SIZE) {
            return Result.fail("文件最大为 10M！");
        }
        String folder = picturePath + "\\" + sampleda.substring(0, 10) + "\\" + instrCode + PropertySlash.getSlash();
        if (!FileUtil.exist(folder)) {
            FileUtil.mkdir(folder);
        }
        Date nameDate=new Date();
        String fileName = sampleda.substring(0, 10) + rptGroup + sampleNo + instrCode + Long.toString(nameDate.getTime())+".png";
        String path = folder + fileName;
        File file1 = FileUtil.writeBytes(file.getBytes(), path);
        if (file1.length() < 0) {
            return Result.fail("文件上传失败");
        }
        LabRptImageTemp labRptImageTemp = new LabRptImageTemp();
        labRptImageTemp.setId(id);
        labRptImageTemp.setImageName(file.getOriginalFilename());
        labRptImageTemp.setImageUrl("/image/" + sampleda.substring(0, 10) + "/" + instrCode + "/" + fileName);
        labRptImageTemp.setImageExt(FileNameUtil.getSuffix(file.getOriginalFilename()));
        //labRptImageTemp.setMaininfoId(maininfoId);
        labRptImageTemp.setRptGroup(rptGroup);
        labRptImageTemp.setSampleda(Timestamp.valueOf(sampleda.substring(0, 10) + " 00:00:00"));
        labRptImageTemp.setSampleNo(sampleNo);
        this.labRptresultService.addMaininfo(rptGroup,sampleda,sampleNo);
        return labRptImageTempService.saveOrUpdateImages(labRptImageTemp);
    }
}

